<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.back()"
      fixed
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
    >
      <!-- <div>{{ this.$route.params.kw }}</div> -->
      <ArtItem
        v-for="item in artList"
        :key="item.art_id"
        :article="item"
        :closable="false"
      ></ArtItem>
    </van-list>
  </div>
</template>

<script>
import { getSuggResultAPI } from '../../api/searchapi.js'
import ArtItem from '../../components/Artitem/Artitem.vue'
import mix from '../../maxins//scroll.js'
export default {
  name: 'SearchResult',
  mixins: [mix],
  // 用来接收路有参数
  props: ['kw'],
  data () {
    return {
      artList: [],
      page: 1,
      // 是否正在请求数组
      loading: true,
      // 所有数据是否加载完毕
      finished: false
    }
  },
  watch: {
    kw () {
      this.artList = []
      this.page = 1
      this.loading = false
      this.finished = false
      this.initSearchResult()
    }
  },
  created () {
    this.initSearchResult()
  },

  methods: {
    async initSearchResult () {
      const { data: res } = await getSuggResultAPI(this.kw, this.page)
      if (res.message === 'OK') {
        console.log(res.data.results)
        // 判断刷新过后是否还有数据,如果没有则将this.finished改为true
        if (res.data.results.length === 0) { this.finished = true }
        // 将新的数据拼接到旧数据的后面,然后遍历即可
        this.artList = [...this.artList, ...res.data.results]
        // this.artList = res.data.results
        this.loading = false
      }
    },
    // 快滚动到底部的时候,会自动调用这个函数
    onLoad () {
      this.page++
      this.initSearchResult()
    }

  },
  components: {
    ArtItem
  }

}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
